package com.lanou.controller;

import com.google.gson.Gson;
import com.lanou.service.SelectService;
import com.lanou.utils.AjaxResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "IndexServlet",urlPatterns = "/index")
public class IndexServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
    private SelectService selectService = new SelectService();
    //显示所有种类即用户
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String id = String.valueOf(session.getAttribute("id"));
        System.out.println("index:"+id);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        AjaxResult ar = new AjaxResult();
        if ("null".equals(id)){
            List list = selectService.selectCategory();
            ar.setCode(1002);
            ar.setMsg("未登录");
            ar.setCategory(list);
            out.write(new Gson().toJson(ar));
        }else {
            int uid = Integer.parseInt(id);
            AjaxResult arlogin = selectService.selectCateAndusername(uid);
            out.write(new Gson().toJson(arlogin));
        }
    out.close();

    }
}
